window.location অবজেক্টটি বর্তমান পেজের ঠিকানা(URL) খুঁজে পেতে এবং ব্রাউজারে নতুন পেজ খুলতে ব্যবহার করা যেতে পারে।
window.location অবজেক্টটি window উপসর্গ(prefix) ছাড়াও লিখা যায়।
উদাহরণঃ
window.location.href প্রোপার্টিটি বর্তমান পেজের অবস্থান(URL) রিটার্ন করে। অর্থাৎ এই প্রোপার্টিটি ব্যবহার করে আমরা বর্তমানে যে পেজে রয়েছি সেই পেজের অবস্থান দেখতে পারি।
<!DOCTYPE html>
<html>
<head>
<title>জাভাস্ক্রিপ্ট উদাহরণ</title>
</head>
<body>
<p id="test"></p>
<script>
document.getElementById("test").innerHTML = "Page location: " + window.location.href;
</script>
</body>
</html>
window.location.hostname প্রোপার্টিটি ইন্টারনেট হোস্টের(বর্তমান পেজের) নাম রিটার্ন করে।
<!DOCTYPE html>
<html>
<head>
<title>জাভাস্ক্রিপ্ট উদাহরণ</title>
</head>
<body>
<p id="test"></p>
<script>
document.getElementById("test").innerHTML = "Page hostname: " + window.location.hostname;
</script>
</body>
</html>
window.location.pathname প্রোপার্টিটি বর্তমান পেজের পথ রিটার্ন করে।
<!DOCTYPE html>
<html>
<head>
<title>জাভাস্ক্রিপ্ট উদাহরণ</title>
</head>
<body>
<p id="test"></p>
<script>
document.getElementById("test").innerHTML = "Page path: " + window.location.pathname;
</script>
</body>
</html>
window.location.protocol প্রোপার্টিটি পেজের ওয়েব প্রোটোকল রিটার্ন করে।
<!DOCTYPE html>
<html>
<head>
<title>জাভাস্ক্রিপ্ট উদাহরণ</title>
</head>
<body>
<p id="test"></p>
<script>
document.getElementById("test").innerHTML = "Page protocol: " + window.location.protocol;
</script>
</body>
</html>
window.location.assign() মেথডটি একটি নতুন ডকুমেন্ট লোড করে।
<!DOCTYPE html>
<html>
<head>
<title>জাভাস্ক্রিপ্ট উদাহরণ</title>
</head>
<body>
<button onclick="myFunc()">ক্লিক করুন</button>
<script>
function myFunc() {
window.location.assign("../index.php");
}
</script>
</body>
</html>
Read more